// pages/editaddress/editaddress.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid: '',
    list: {},
    id:'',
    regionList: [
      {
        id: 1,
        name: '沧州市运河区'
      }, {
        id: 2,
        name: '沧州市新华区'
      }, {
        id: 3,
        name: '沧州市开发区'
      }, {
        id: 4,
        name: '沧州市高新区'
      }
    ],
    regionIndex: 4,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if(options.id){
      var list={};
      list.address=options.address;
      list.name=options.name;
      list.mobile=options.mobile;
      list.area=options.area;
      list.id=options.id;
      that.setData({
        list:list
      })
    }
    if(options.flag){
      that.setData({
        flag:options.flag
      })
    }
  },
  bindRegionChange: function (e) {
    this.setData({
      regionIndex: e.detail.value
    })
  },

  formSubmit:function(e){
    var that=this;
    var list=e.detail.value;
    var flag=this.data.flag;
    if(list.name==""){
      wx.showToast({
        title: '请填写收货人姓名',
        icon:'none',
      })
    }else if (list.mobile == "") {
      wx.showToast({
        title: '请填手机号',
        icon: 'none',
      })
    } else if ((!(/^1[3456789]\d{9}$/.test(parseInt(list.mobile))))){
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
      })
    } else if (list.area == "") {
      wx.showToast({
        title: '请选择区域',
        icon: 'none',
      })
    }else if (list.address == "") {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
      })
    }else{
      //上传保存
      if(that.data.list.id){
        // 修改
        list.id=that.data.list.id;
        app.func.postReq("/text/index.php/home/address/editaddress", list, function (res) {
          wx.navigateBack({})
        });
      }else{
        // 新增
        app.func.postReq("/text/index.php/home/address/addsaddress", list, function (res) {
          if (that.data.flag) {
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            list.id=res;
            prevPage.setData({
              address: list
            })
          }
          wx.navigateBack({})
          
        });
      }
    }

  }
})